# AIQuant 量化投研系统 Docker镜像
# 多阶段构建，优化镜像大小和安全性

# =============================================================================
# 基础镜像阶段
# =============================================================================
FROM python:3.11-slim as base

# 设置环境变量
ENV PYTHONUNBUFFERED=1 \
    PYTHONDONTWRITEBYTECODE=1 \
    PIP_NO_CACHE_DIR=1 \
    PIP_DISABLE_PIP_VERSION_CHECK=1 \
    DEBIAN_FRONTEND=noninteractive

# 安装系统依赖
RUN apt-get update && apt-get install -y --no-install-recommends \
    build-essential \
    curl \
    git \
    libpq-dev \
    libffi-dev \
    libssl-dev \
    pkg-config \
    && rm -rf /var/lib/apt/lists/*

# 创建应用用户
RUN groupadd -r aiquant && useradd -r -g aiquant aiquant

# 设置工作目录
WORKDIR /app

# =============================================================================
# 依赖安装阶段
# =============================================================================
FROM base as dependencies

# 复制依赖文件
COPY requirements.txt requirements-dev.txt ./

# 安装Python依赖
RUN pip install --upgrade pip setuptools wheel && \
    pip install -r requirements.txt

# =============================================================================
# 开发环境阶段
# =============================================================================
FROM dependencies as development

# 安装开发依赖
RUN pip install -r requirements-dev.txt

# 复制源代码
COPY . .

# 设置权限
RUN chown -R aiquant:aiquant /app

# 切换到应用用户
USER aiquant

# 暴露端口
EXPOSE 8000 8501 5555

# 健康检查
HEALTHCHECK --interval=30s --timeout=10s --start-period=5s --retries=3 \
    CMD python scripts/health_check.py --timeout 5 || exit 1

# 默认命令
CMD ["python", "scripts/start_system.py", "all"]

# =============================================================================
# 生产环境阶段
# =============================================================================
FROM base as production

# 只安装生产依赖
COPY requirements.txt ./
RUN pip install --upgrade pip setuptools wheel && \
    pip install -r requirements.txt && \
    pip cache purge

# 复制应用代码
COPY aiquant/ ./aiquant/
COPY scripts/ ./scripts/
COPY config/ ./config/
COPY .env.example ./

# 创建必要的目录
RUN mkdir -p logs data backups && \
    chown -R aiquant:aiquant /app

# 切换到应用用户
USER aiquant

# 暴露端口
EXPOSE 8000

# 健康检查
HEALTHCHECK --interval=30s --timeout=10s --start-period=5s --retries=3 \
    CMD python scripts/health_check.py --timeout 5 || exit 1

# 生产环境启动命令
CMD ["python", "scripts/start_system.py", "api", "--workers", "4"]

# =============================================================================
# Web仪表板专用镜像
# =============================================================================
FROM base as web

# 安装Web相关依赖
COPY requirements.txt ./
RUN pip install --upgrade pip setuptools wheel && \
    pip install -r requirements.txt && \
    pip cache purge

# 复制Web相关代码
COPY aiquant/ ./aiquant/
COPY scripts/ ./scripts/
COPY config/ ./config/
COPY .env.example ./

# 创建必要的目录
RUN mkdir -p logs data && \
    chown -R aiquant:aiquant /app

# 切换到应用用户
USER aiquant

# 暴露Streamlit端口
EXPOSE 8501

# 健康检查
HEALTHCHECK --interval=30s --timeout=10s --start-period=5s --retries=3 \
    CMD curl -f http://localhost:8501/_stcore/health || exit 1

# Web仪表板启动命令
CMD ["python", "scripts/start_system.py", "web", "--host", "0.0.0.0"]

# =============================================================================
# Worker专用镜像
# =============================================================================
FROM base as worker

# 安装Worker相关依赖
COPY requirements.txt ./
RUN pip install --upgrade pip setuptools wheel && \
    pip install -r requirements.txt && \
    pip cache purge

# 复制Worker相关代码
COPY aiquant/ ./aiquant/
COPY scripts/ ./scripts/
COPY config/ ./config/
COPY .env.example ./

# 创建必要的目录
RUN mkdir -p logs data && \
    chown -R aiquant:aiquant /app

# 切换到应用用户
USER aiquant

# Worker启动命令
CMD ["python", "scripts/start_system.py", "worker"]

# =============================================================================
# 镜像标签和元数据
# =============================================================================
LABEL maintainer="AIQuant Team <team@aiquant.com>" \
    version="1.0.0" \
    description="AIQuant 量化投研系统" \
    org.opencontainers.image.title="AIQuant" \
    org.opencontainers.image.description="AI-powered quantitative investment research system" \
    org.opencontainers.image.version="1.0.0" \
    org.opencontainers.image.vendor="AIQuant Team" \
    org.opencontainers.image.licenses="MIT" \
    org.opencontainers.image.source="https://github.com/your-org/aiquant"